package cn.lby.tp.downloadtest.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.lby.tp.downloadtest.BaseApplication;
import cn.lby.tp.downloadtest.R;
import cn.lby.tp.downloadtest.fragment.DownCurrentFragment;
import cn.lby.tp.downloadtest.fragment.DownDoneFragment;

public class OkDownLoadingActivity extends BaseApplication implements DownDoneFragment.Callbacks{


    @BindView(R.id.viewPager2)
    ViewPager2 viewPager2;

    @BindView(R.id.tab)
    TabLayout tabLayout;

    @BindView(R.id.edit)
    TextView edit;


    @BindView(R.id.radio)
    CheckBox qx;

    @BindView(R.id.del)
    TextView del;

    @BindView(R.id.bottomMenu)
    RelativeLayout bottomMenu;



    private List<Fragment> fragmentList;
    private DownCurrentFragment currentFragment;
    private DownDoneFragment doneFragment;


    // 任务全部开始
    public void startAllTask() {

//        DownCurrentFragment fragment = (DownCurrentFragment) fragmentList.get(0);
//        if (fragment.getFileInfoList().size()==0) {
//            Toast.makeText(this, "没有可下载的任务", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        boolean b = downAll.isSelected();
//        if (b) {
//            downAll.setText("全部开始");
//            downAll.setSelected(false);
//        } else {
//            downAll.setText("全部暂停");
//            downAll.setSelected(true);
//        }
//        fragment.startDownAllTask();

    }

    // 任务全部取消
    public void cancelAllTask() {
//        DownCurrentFragment fragment = (DownCurrentFragment) fragmentList.get(0);
//        if (fragment.getFileInfoList().size()==0) {
//            Toast.makeText(this, "没有可取消的任务", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        fragment.cancelDownAllTask();

    }

    /**
     * 添加任务按钮点击事件
     * @param view
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void addTask(View view) {
//        String name = fileName.getText().toString();
//        String url = downUrl.getText().toString();
//        if ("".equals(name) || "".equals(url)) {
//            Toast.makeText(this, "文件名或下载地址不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        DownCurrentFragment fragment = (DownCurrentFragment) fragmentList.get(0);
//        fragment.addTask(name, url);
//        fragment.recyclerViewNotify();
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_okdownload_down;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewPager();


        edit.setOnClickListener(view -> {
            if (doneFragment != null) {
                if (!view.isSelected()) {
                    edit.setText("取消");
                    edit.setSelected(true);
                    bottomMenu.setVisibility(View.VISIBLE);
                    doneFragment.getAdapter().showSelect();

                } else {
                    edit.setText("编辑");
                    edit.setSelected(false);
                    doneFragment.qbx();
                    bottomMenu.setVisibility(View.GONE);
                }
            }
        });

//        downAll.setOnClickListener(v -> {
//            startAllTask();
//        });
//
//        cancelAll.setOnClickListener(v -> {
//            cancelAllTask();
//        });


        qx.setOnClickListener(view -> {
            doneFragment.qx();
        });

        del.setOnClickListener(view -> {
            doneFragment.commit();
            edit.setText("编辑");
            edit.setSelected(false);
            doneFragment.qbx();
            bottomMenu.setVisibility(View.GONE);

        });

    }

    private void initViewPager() {
        // 禁用预加载

        // Adapter
        fragmentList = new ArrayList<>();
        currentFragment = new DownCurrentFragment();
        doneFragment = new DownDoneFragment();

        fragmentList.add(currentFragment);
        fragmentList.add(doneFragment);
        MyViewPager myViewPager = new MyViewPager(this, fragmentList);
        viewPager2.setAdapter(myViewPager);

        String[] tabArray = {"下载中", "已下载"};
        TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager2, ((tab, position) -> {
            tab.setText(tabArray[position]);
        }));
        mediator.attach();
        
        
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (Fragment fragment : fragmentList) {
                    if (fragment instanceof DownCurrentFragment) {
                        ((DownCurrentFragment) fragment).recyclerViewNotify();
                    }
                    if (fragment instanceof DownDoneFragment) {
                        ((DownDoneFragment) fragment).recyclerViewNotify();
                    }
                }
            }
        });

    }


    // DownCurrentFragment 传递过来的数据
    @Override
    public void onItemSelected() {
        if (doneFragment != null) {

            edit.setText("取消");
            edit.setSelected(true);
            bottomMenu.setVisibility(View.VISIBLE);
            doneFragment.getAdapter().showSelect();
        }
    }

    public void exit(View view) {
        finish();
    }


    static class MyViewPager extends FragmentStateAdapter {
        private List<Fragment> pagerViewList;

        public MyViewPager(@NonNull FragmentActivity fragmentActivity, List<Fragment> pagerViewList) {
            super(fragmentActivity);
            this.pagerViewList = pagerViewList;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return pagerViewList.get(position);
        }

        @Override
        public int getItemCount() {
            return pagerViewList.size();
        }
    }
}